<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/29 0029
 * Time: 上午 9:34
 */

namespace Home\Controller;

use Think\Controller;

class EmplistController extends Controller
{

//    public function __construct()
//    {
//        parent::__construct();
//    }

    public function _initialize()
    {
        if (!session('username')) {
            $this->error('请登录', U('login/index'));
        }
    }

    public function index($curpage = 1, $limit = 5)
    {
        //分页
        //a,初始化分页(http接收)
        //b,获取数据总数


        $empModel = M('emp');
        //获取数据总数
        $count = $empModel->count();
        $this->assign('count', $count);
        $this->assign('curr', $curpage);

//1:0 2:5 3:10
        $emps = $empModel
            ->join("__DEPT__ ON __EMP__.Edeptno = __DEPT__.Dno")
//            ->limit(($curpage-1)*5,$limit)
            ->page($curpage, $limit)
            ->select();
//        return DB::findAll("SELECT * FROM tb_emp e LEFT JOIN tb_dept d ON e.Edeptno = d.Dno ORDER BY e.Eno");

//        $emps = $empModel->select();

        $this->assign('emps', $emps);
        $this->display();
    }

    //添加员工
    public function add()
    {






        //模型实例化M方法
        $depts = M('dept')->select();
//        var_dump($depts);
        $this->assign('depts', $depts);
        $this->display();
    }

    public function doadd()
    {
        // 上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xlsl','xls');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     'Excel/'; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        dump($info);die;
        if (!$info) {
            $this->error($upload->getError());exit;
        }
        $file_name =  $upload->rootPath.$info['files']['savepath'].$info['files']['savename'];

        // exl
        $exl = $this->excelIn($file_name);




//        $name = I('Ename');
//        $age = I('Eage');
//        echo $name . $age;
        //通过create创建数据
        $Emp = M('emp');
        $data = $Emp->create();
        $result = $Emp->add($data);
        if ($result) {
            $this->success('添加成功', U('emplist/index'));
        } else {
            $this->error('添加失败');
        }
    }
    // 处理上传exl数据
    function excelIn($file_name){
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.IOFactory');
        if ('xlsx' == substr(strrchr($file_name,"."),1)) {
            $objReader=\PHPExcel_IOFactory::createReader('Excel2007');
        }else{
            $objReader=\PHPExcel_IOFactory::createReader('Excel5');
        }
        $objReader=\PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel=$objReader->load($file_name);//$file_url即Excel文件的路径
        $sheet=$objPHPExcel->getSheet(0);//获取第一个工作表
        $highestRow=$sheet->getHighestRow();//取得总行数
        $highestColumn=$sheet->getHighestColumn(); //取得总列数
        $res_data=NULL;
        $res_top=NULL;
        for($j=2;$j<=$highestRow;$j++){//从第一行开始读取数据
            $str='';
            for($k='B';$k<=$highestColumn;$k++){            //从A列读取数据
                $res_data[$j-1][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
            }
        }
        unlink($file_url);
        return $res_data;
    }

    //删除
    public function delete($no)
    {
//        $result = M('emp')->where("Eno={$no}")->delete();
        $result = M('emp')->delete($no);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //编辑
    public function edit($Eno)
    {
        //1.获取员工信息
        $emp = M('emp')->where("Eno={$Eno}")->find();
        //2，查找部门
        $depts = M('dept')->select();
        //3,页面展示
        $this->assign('emp', $emp);
        $this->assign('depts', $depts);
        $this->display();
    }

    public function doedit()
    {
        $Emp = M('emp');
        $data = $Emp->create();
//        var_dump($data);
        $result = $Emp->save($data);
        if ($result) {
            $this->success('修改成功', U('index'));
        } else {
            $this->error('修改失败');
        }

    }
}